package ru.urfc.portal.ui.client.widgets;

import java.util.Date;

import com.extjs.gxt.ui.client.widget.form.DateField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Widget;

public class DateFilter extends BaseFilter {

	interface Binder extends UiBinder<Widget, DateFilter> {}
	private static Binder binder = GWT.create(Binder.class);
	@UiField DateField fromDate;
	@UiField DateField toDate;
	@UiField Element placeForSearchButton;
	
	public DateFilter() {
		initWidget(binder.createAndBindUi(this));
		fromDate.setValue(prevMonth1stDay(new Date()));
		toDate.setValue(new Date());
		
		descriptor.addTextField(FILTER_FROM_DATE, fromDate);
		descriptor.addTextField(FILTER_TO_DATE, toDate);
	}

	public DateFilter(boolean test) {		
	}	
	
	@SuppressWarnings("deprecation")
	Date prevMonth1stDay(Date date) {
		Date result = new Date(date.getTime());
		result.setMonth(date.getMonth() - 1); // have no Calendar in GWT JRE
		result.setDate(1);
		return result;
	}

	@Override
	protected Element getPlaceForSearchButton() {
		return placeForSearchButton;
	}

}
